package com.qiuqiu.service.impl;

import com.qiuqiu.dao.TodoListDao;
import com.qiuqiu.entity.TodoList;
import com.qiuqiu.service.TodoListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author ：张艳杰
 * @description：TODO
 * @date ：Created in 2021/8/22 10:39
 * @modified By：
 * @version: 1.0
 */
@Service
@Transactional
public class TodoListServiceImpl implements TodoListService {

    @Resource
    private TodoListDao todoListDao;

    @Override
    public List<TodoList> getTodoList() {
        return this.todoListDao.findAll();
    }

    @Override
    public TodoList addList(String name) {
        TodoList todoList = new TodoList();
        todoList.setTaskName((name));
        todoList.setIsCompleted(0);
        todoList.setIsEditing(0);
        TodoList save = this.todoListDao.save(todoList);
        return save;
    }

    @Override
    public void delTodoList(Integer id) {
        this.todoListDao.deleteById(id);
    }

    @Override
    public TodoList isCompleted(Integer id, Integer status) {
        TodoList byId = this.todoListDao.getById(id);
        byId.setIsCompleted(status);
        this.todoListDao.save(byId);
        return byId;
    }
    @Override
    public void clearCompleted() {
        this.todoListDao.deleteByIsCompleted(1);
    }

    @Override
    public TodoList isEditing(Integer id, Integer status) {
        Optional<TodoList> byId = this.todoListDao.findById(id);
        TodoList todoList = byId.get();
        todoList.setIsEditing(status);
        this.todoListDao.save(todoList);
        return todoList;
    }

    @Override
    public TodoList updateTaskName(Integer id, String taskName) {
        Optional<TodoList> byId = this.todoListDao.findById(id);
        TodoList todoList = byId.get();
        todoList.setTaskName(taskName);
        todoList.setIsEditing(0);
        this.todoListDao.save(todoList);
        return todoList;
    }
}
